package com.wanghlme.leetcodecn;

/**
 * <p>剑指 Offer 04. 二维数组中的查找</p>
 * <p>JZ04->main0240</p>
 *
 * @author wanghlme
 * @version 1.0
 */
public class LC_JZ04 {
    public static void main(String[] args) {
        int[][] arr = {{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}};
//        int[][] arr = {{1, 1}};
        System.out.println(new Solution().findNumberIn2DArray(arr, 2));
    }

    static class Solution {
        public boolean findNumberIn2DArray(int[][] matrix, int target) {
            if (matrix.length == 0) return false;
            int rowU = 0;
            int rowD = matrix.length - 1;
            int colL = 0;
            int colR = matrix[0].length - 1;
            while (rowU <= rowD && colR >= colL) {
                if (target < matrix[rowU][colR]) {
                    colR--;
                } else if (target > matrix[rowU][colR]) {
                    rowU++;
                } else {
                    return true;
                }
            }
            return false;
        }
    }
}
